﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CashNote
{
    public partial class NoteList : Form
    {
        public NoteList()
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(" select ID, input_date, neirong, biaoti, beizhu, modify_date, staff_name ");
            sb.Append(" from t_mem ");
            sb.Append(" where (staff_id = '" + Global.StrUser + "') order by ID desc ");

            connTest conntest = new connTest();

            DataSet ds = conntest.Select(sb.ToString(), Global.Conn);

            DataTable dt = ds.Tables[0].Copy();

            this.dataGridView1.DataSource = dt;

            ds.Dispose();
            dt.Dispose();

            // Resize the DataGridView columns to fit the newly loaded content.
            //dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);


        }

        void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            // Clear the row error in case the user presses ESC.   
            dataGridView1.Rows[e.RowIndex].ErrorText = String.Empty;
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            string id = dataGridView1.CurrentRow.Cells["id"].Value.ToString();
            string nr = dataGridView1.CurrentRow.Cells["neirong"].Value.ToString();
            string bz = dataGridView1.CurrentRow.Cells["beizhu"].Value.ToString();
            string bt = dataGridView1.CurrentRow.Cells["biaoti"].Value.ToString();

            NoteForm nf = new NoteForm();
            nf.setDate(Convert.ToDouble(id), nr,bt,bz);
            nf.ShowDialog();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                this.contextMenuStrip1.Show(this.dataGridView1, new Point(e.X, e.Y));
            }
        }

        private bool deleteRow(double d)
        {
            bool b = false;
            StringBuilder sb = new StringBuilder();
            sb.Append("delete from t_mem where id = "+ d +" ");

            connTest ct =new connTest();

            if (ct.Delete(sb.ToString(), Global.Conn))
            {
                b = true;
            }
            else
            {
                b = false;
            }

            return b;
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            double id = Convert.ToDouble(dataGridView1.CurrentRow.Cells["id"].Value.ToString());

            if (deleteRow(id))
            {
                dataGridView1.Rows.RemoveAt(this.dataGridView1.CurrentRow.Index);
            }
            else
            {
                MessageBox.Show("删除出错");
            }

        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }

    }
}
